package com.aote.entity.action;

import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.aote.mvc.event.Action;
import com.aote.util.ThreadLocals;
import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.baseadapter.AdvancedSimpleEntity;
import com.browsesoft.baseadapter.BasicEntity;
import com.browsesoft.htmlcomponent.HTMLPage;

public class ExpirationNumberAction extends Action {
	public void performAction(Element config, HttpServletRequest request,
			Map attrs) {
		// 得到模型
		AdvancedSimpleEntity model = (AdvancedSimpleEntity) component
				.getModel();
		NodeList nodes = config.getElementsByTagName("attr");
		for (int i = 0; i < nodes.getLength(); i++) {
			Element node = (Element) nodes.item(i);
			String name = node.getAttribute("expiration");
			String entityType = node.getAttribute("entitytype");
			if (model.getEntityType().equals(entityType)) {
				((BasicEntity) model).expirationNumber(name);
				continue;
			}
			String parentid = (String) model.getAttributes().get("parentid");
			HTMLPage page = (HTMLPage) ThreadLocals.page.get();
			Collection list = EntityManager.getInstance().getEntities(
					entityType, "parentid", parentid, page);
			if (list == null || list.isEmpty()) {
				continue;
			}
			Entity ent = (Entity) list.iterator().next();
			((BasicEntity) ent).expirationNumber(name);
		}
	}
}
